<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Application\Model\Category;
use Admin\Form\MenuForm;
use Admin\Form\NewsForm;
use Application\Model\vn3dUtil;

class OrderController extends AbstractActionController
{

    protected $CategoryTable;

    protected $ProductTable;

    protected $ImageTable;

    protected $OrderTable;

    protected $OrderProductTable;

    public function getOrderTableTable()
    {
        if (! $this->OrderTable) {
            $sm = $this->getServiceLocator();
            $this->OrderTable = $sm->get('Application\Model\OrderTable');
        }
        return $this->OrderTable;
    }

    public function getOrderProductTable()
    {
        if (! $this->OrderProductTable) {
            $sm = $this->getServiceLocator();
            $this->OrderProductTable = $sm->get('Application\Model\OrderProductTable');
        }
        return $this->OrderProductTable;
    }

    public function getCategoryTable()
    {
        if (! $this->CategoryTable) {
            $sm = $this->getServiceLocator();
            $this->CategoryTable = $sm->get('Application\Model\CategoryTable');
        }
        return $this->CategoryTable;
    }

    public function getProductTable()
    {
        if (! $this->ProductTable) {
            $sm = $this->getServiceLocator();
            $this->ProductTable = $sm->get('Application\Model\ProductTable');
        }
        return $this->ProductTable;
    }

    public function getImageTable()
    {
        if (! $this->ImageTable) {
            $sm = $this->getServiceLocator();
            $this->ImageTable = $sm->get('Application\Model\ImageTable');
        }
        return $this->ImageTable;
    }

    public function indexAction()
    {
        if (! vn3dUtil::checkAuth(($this))) {

            return $this->redirect()->toRoute('user', array(
                "action" => "index"
            ));
        }
        ;
        $order_by = $this->params()->fromRoute('order_by') ? $this->params()->fromRoute('order_by') : 'id';
        $order = $this->params()->fromRoute('order') ? $this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
        $page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
        // grab the paginator from the AlbumTable
        $paginator = $this->getOrderTableTable()->fetchAll(true);
        // set the current page to what has been passed in query string, or to 1 if none set
        $paginator->setCurrentPageNumber((int) $this->params()
        		->fromQuery('page', $page));
        // set the number of items per page to 10
        $paginator->setItemCountPerPage(5);

        $this->setLayoutAdmin($this);
        return new ViewModel(array(
        		'orders' => $paginator,
            "currentpage"=>$page
        ));



    }

    private function  setLayoutAdmin($controller){
    	$controller->layout("/layout/admin");
    	$controller->forward()->dispatch("Application\Controller\Store", array(
    			'action' => 'search',
    			'controller' => 'Application\Controller\Store'
    	));
    }
}
